<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="zjm">
          <a-input v-model:value="formState.zjm" class="w-[300px]" placeholder="部署内容" />
        </a-form-item>
        <a-form-item name="dateRange" label="部署日期">
          <a-range-picker v-model:value="formState.dateRange" class="w-[300px]" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex">
        <a-button class="mb-2 mr-2" type="primary" @click="onEditOpen"> 新增 </a-button>
        <BaseExport type="防疫防护" name="防护部署" code="000688" :search-params="formState">导出</BaseExport>
        <div class="absolute top-[-9999px] hidden">
          <PrintView id="printMe" :row-data="rowData" />
        </div>
      </div>
      <BaseTable fill :data-source="tableData" :loading="loading" column-code="000688" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-space>
              <a-button type="link" size="small" @click="onEditOpen(record)">修改</a-button>
              <BaseButton confirm-text="是否删除?" type="text" danger size="small" @click="onDelete(record)"> 删除 </BaseButton>
              <a-button v-print="{ id: 'printMe' }" type="link" size="small" @click="rowData = record"> 打印 </a-button>
            </a-space>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="getData" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import { post } from '@/utils/request'
import BaseTable from '@/components/BaseTable'
import BaseButton from '@/components/BaseButton'
import Edit from './components/Edit'
import PrintView from './components/PrintView'

const editOpen = ref(false)
const rowData = ref({})
const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})

const { loading, tableData, pagination, getData } = useTable({
  url: '/war/epidemicHygieneProtection/getProtectionDeploymentResponseList',
  immediate: true,
  formatParams: () => {
    return {
      ...formState.value,
      startDate: formState.value.dateRange?.[0],
      endDate: formState.value.dateRange?.[1]
    }
  }
})

const onEditOpen = (record = {}) => {
  rowData.value = { ...record }
  editOpen.value = true
}

// 删除
const onDelete = async (record) => {
  const res = await post('/war/epidemicHygieneProtection/doDelProtectionDeployment', { id: record.id })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}
</script>
